package ph.com.gs3.formalistics.model.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import android.content.Context;

public class SupportDAO extends FileDataAccessObject {

	public static final String TAG = SupportDAO.class.getSimpleName();

	public static final String ERROR_LOG_FILE = "error";
	public static final String WARNING_LOG_FILE = "warning";
	public static final String DEBUG_LOG_FILE = "debug";

	private String applicationPathString;

	public SupportDAO(Context context) {
		super(context);

		applicationPathString = context.getApplicationContext().getFilesDir().getAbsolutePath();
	}

	public void writeLog(String log) {

	}

	public List<String> getLogFileNames() {

		List<String> logFileNames = new ArrayList<>();
		File applicationPathFile = new File(applicationPathString);

		for (File file : applicationPathFile.listFiles()) {
			if (file.isFile()) {
				FLLogger.d(TAG, file.getName());
				logFileNames.add(file.getName());
			}
		}

		return logFileNames;

	}

	public String getLogContents(String logFileName) {

		File logFile = new File(applicationPathString + "/" + logFileName);

		FileInputStream fis = null;
		try {
			// throws FileNotFoundException
			fis = new FileInputStream(logFile);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader bufferedReader = new BufferedReader(isr);
			StringBuilder sb = new StringBuilder();
			String line;
			// throws IOException
			while ((line = bufferedReader.readLine()) != null) {
				sb.append(line);
			}

			return sb.toString();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return null;

	}

}
